<template>
<div id="loginBox">
  <el-card class="box-card" id="login">
    <div slot="header" class="clearfix">
      <h2>用户登录</h2>
    </div>
    <el-form
      :model="ruleForm2"
      status-icon
      :rules="rules2"
      ref="ruleForm2"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="账号" prop="username">
        <el-input type="text" v-model="ruleForm2.username" autocomplete="off" placeholder="请输入用户名 长度在 6 到 12 个字符" ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input type="password" v-model="ruleForm2.pass" autocomplete="off" placeholder="请输入密码 长度在 6 到 18 个字符"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm2')">登录</el-button>
        <el-button @click="resetForm('ruleForm2')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</div>
</template>

<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm2.checkPass !== "") {
          this.$refs.ruleForm2.validateField("checkPass");
        }
        callback();
      }
    };

    return {
      ruleForm2: {
        username: "", //用户名
        pass: "" //密码
      },
      rules2: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }, //验证是否为空
          { min: 6, max: 12, message: "长度在 6 到 12 个字符", trigger: "blur" } //验证用户名长度
        ],
        pass: [
        //   { validator: validatePass, trigger: "blur" },
          { required: true,min: 6, max: 18, message: "长度在 6 到 18 个字符", trigger: "blur" } //验证密码长度
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          alert("登录成功!");
          this.$router.push("/");
        } else {
          console.log("登录失败!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style>
/* 背景图 */
#loginBox{
    background: url("../assets/ban.jpg") center center;
    height:100%;
}
#login {
  width: 520px;
  height: 260px;

  /* 盒子居中 */
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  margin: auto;
}

.el-card__header h2{
    margin:0 !important;
}
</style>

